/* QAD_Item-SiteATPHorizonUpdate.p - Update and display Item/Item-Site Horizon*/
/* Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA.                      */
/* All rights reserved worldwide.  This is an unpublished work.               */
/* $Id:: QAD_TransactionsAccountingReport.p 1046 2010-09-10 09:08:19Z jvz  $: */
/*                                                                            */
/* Report matches legacy utatpah.p for use by QAD Reporting Framework.        */
/*                                                                            */

{mfsubdirs.i}
{{&US_BBI}mfdeclre.i}
{{&US_BBI}gplabel.i}

{com/qad/shell/report/dsReportRequest.i}
{com/qad/shell/report/ReportConstants.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/

/* TEMP-TABLE DEFINITION BLOCK */

define temp-table ttATPHorizon no-undo
   field pt_part        like pt_mstr.pt_part
   field pt_desc1       like pt_mstr.pt_desc1
   field pt_atp_horizon like pt_mstr.pt_atp_horizon
   field unique_id      as   integer
.

define temp-table ttReportConditions no-undo
   field pt_prod_line like pt_mstr.pt_prod_line
   field pt_part_type like pt_mstr.pt_part_type
   field pt_abc       like pt_mstr.pt_abc
   field pt_group     like pt_mstr.pt_group
   field pt_dsgn_grp  like pt_mstr.pt_dsgn_grp
   field pt_promo     like pt_mstr.pt_promo
   field pt_comm_code like pt_mstr.pt_comm_code
   field pt_pm_code   like pt_mstr.pt_pm_code
   field pt_buyer     like pt_mstr.pt_buyer
.

define temp-table ttReportOptions no-undo
   field pt_site   like pt_mstr.pt_site
   field update_yn as   mfc_ctrl.mfc_logical
.

define dataset dsReportResults for ttATPHorizon, ttReportConditions, ttReportOptions.

/*============================================================================*/
/* *****************************  MAIN BLOCK  ******************************* */
/*============================================================================*/
define input parameter runReport    as logical no-undo.
define input parameter reportHandle as handle  no-undo.
define input parameter dataset      for dsReportRequest.
define output parameter dataset-handle phReportResults.

{com/qad/shell/report/reporting.i}

define variable vhDS      as handle  no-undo.
define variable unique_id as integer no-undo.

empty temp-table ttATPHorizon       no-error.
empty temp-table ttReportConditions no-error.
empty temp-table ttReportOptions    no-error.

for first ttReportRequest no-lock:

   vhDS = dataset dsReportResults:handle.
   run FillMetaData in reportHandle(
      vhDS, "QAD_Item-SiteATPHorizonUpdate.meta.xml").

   run SetReportMetaData (input reportHandle).

   if runReport then do:
      run RunReport
         (output dataset-handle phReportResults).
   end.

   delete object phReportResults no-error.

end.

PROCEDURE SetReportMetaData:

   define input parameter  ipReportHandle as handle.

   run SetFieldMetaParameter in ipReportHandle(
         "ttReportOptions", "pt_site", "DefaultValue", global_site).

   run SetFieldMetaParameter in ipReportHandle ("ttReportOptions",
            "update_yn", "DataFormat", getLabel("YES/NO")).

END PROCEDURE. /* PROCEDURE ModifyMetaData */

/* DATA RETRIEVING LOGIC BLOCK */
PROCEDURE RunReport:

   define output parameter dataset-handle phReportResults.

   /* RETRIEVE THE DATA FROM DATABASE */
   define variable queryString   as character no-undo.
   define variable hMainQuery    as handle    no-undo.
   define query    MainQuery     for pt_mstr.
   define variable site          like pt_mstr.pt_site no-undo.
   define variable pm            like pt_mstr.pt_pm_code.
   define variable l_update_yn   like mfc_ctrl.mfc_logical  no-undo.
   define variable l_atp_horizon like pt_mstr.pt_atp_horizon no-undo.
   define variable disp_value_of_atp_horizon 
                                 like pt_mstr.pt_atp_horizon no-undo.
   define buffer   b_ptp_det     for ptp_det.
   define buffer   b_pt_mstr     for pt_mstr
   
   assign
      hMainQuery           = query MainQuery:handle
      site                 = GetFilterValue("ttReportOptions", "pt_site", "")
      pm                   = GetFilterValue("ttReportConditions", "pt_pm_code", "")
      l_update_yn          = IsTrueValue("ttReportOptions", "update_yn")
      l_atp_horizon        = Integer(GetFilterValue("ttATPHorizon", "pt_atp_horizon", 0).

   queryString = "for each pt_mstr no-lock where pt_domain = "
                  + quoter(global_domain).

   run AddSomeConditions in reportHandle
      ("pt_mstr", "pt_site,pt_pm_code", input-output queryString).

   queryString = queryString + ":".

   mainRepeat:
   do transaction on error undo:
      
      hMainQuery:query-prepare(queryString).
      hMainQuery:query-open().
      hMainQuery:get-next().

      repeat while not hMainQuery:query-off-end:

         if site <> ""
         then do:

            for first ptp_det
               where ptp_det.ptp_domain = global_domain
               and   ptp_part           = pt_part
               and   ptp_site           = site
            no-lock:
            end. /* FOR FIRST ptp_det */

            if (available ptp_det
               and ptp_pm_code <> pm
               and pm          <> "")
               or (not available ptp_det)
            then do:
               hMainQuery:get-next().
               next.
            end.
            
            if l_update_yn:
               find first b_ptp_det where recid(b_ptp_det) = recid(ptp_det)
               exclusive-lock no-wait no-error.

               if locked(b_ptp_det)
               then do:
                  {{&US_BBI}pxmsg.i &MSGNUM=1763 &ERRORLEVEL=3}
                  undo mainRepeat , leave mainRepeat.
               end. /* if locked */
                  
               ptpdet.ptp_atp_horizon = l_atp_horizon.
            end. /* if l_update_yn */
           
            disp_value_of_atp_horizon = if l_update_yn
                                        then l_atp_horizon
                                        else ptp_atp_horizon.

            run CreateDataForttATPHorizon(pt_part, pt_desc1,
                  disp_value_of_atp_horizon).

         end. /* IF site <> "" */
         else do:

            if (available pt_mstr
               and pt_mstr.pt_pm_code <> pm
               and pm                 <> "")
               or (not available pt_mstr
               and pt_mstr.pt_pm_code  <> pm
               and pm                  <> "")
            then do:
               hMainQuery:get-next().
               next.
            end.

            if l_update_yn
            then do:
               find first b_pt_mstr where recid(b_pt_mstr) = recid(pt_mstr)
               exclusive-lock no-wait no-error.
               
               if locked b_pt_mstr
               then do:
                  {{&US_BBI}pxmsg.i &MSGNUM=1763 &ERRORLEVEL=3}
                  undo mainRepeat , leave mainRepeat.
               end. /* if locked b_pt_mstr */
              
               b_pt_mstr.pt_atp_horizon = l_atp_horizon.
            end. /* if l_update_yn */

            disp_value_of_atp_horizon = if l_update_yn
                                        then l_atp_horizon
                                        else pt_mstr.pt_atp_horizon.

            run CreateDataForttATPHorizon(pt_part, pt_desc1,
                  disp_value_of_atp_horizon).
         end. /* else site = "" */

         hMainQuery:get-next().
      end. /* REPEAT QUERY */
      
      hMainQuery:query-close().
   end. /* do transaction */

   delete object hMainQuery.
   phReportResults = dataset dsReportResults:handle.
   delete object phReportResults no-error.

END PROCEDURE.

PROCEDURE CreateDataForttATPHorizon:

   define input parameter part         like pt_mstr.pt_part no-undo.
   define input parameter desc1        like pt_mstr.pt_desc1 no-undo.
   define input parameter atp_horizon  like pt_mstr.pt_atp_horizon no-undo.

   create ttATPHorizon.
   assign
      unique_id                  = unique_id + 1
      ttATPHorizon.pt_part       = part
      ttATPHorizon.pt_desc1      = desc1
      ttATPHorizon.atp_horizon   = atp_horizon
      ttATPHorizon.unique_id     = unique_id
   .

END PROCEDURE /* PROCEDURE CreateDataForttATPHorizon */
